/*

This file is part of roomyak.

Copyright (C) 2008 Pixecur Technologies Incorporated (Esotera).
Visit Esotera at http://www.esoteras3.com
 
roomyak is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3, or (at your option)
any later version.
 
roomyak is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
General Public License for more details.
 
You should have received a copy of the GNU General Public License
along with roomyak; see the file COPYING.  If not, write to 
Pixecur Technologies, 213-31 Peet Street, St. John's, NL, A1B 3W8. 
 
Linking this library statically or dynamically with other modules is
making a combined work based on this library.  Thus, the terms and
conditions of the GNU General Public License cover the whole
combination.
 
*/

package com.datasentinel.util;

import java.util.Stack;
import java.util.logging.Logger;

/**
 * @author rickp
 * 
 */
public class TreeTraversal implements Runnable
{
	private Object toTraverse;
	private TreeTraversalCallback traversalCallback;
	private TreeTraversalSupport treeTraversalSupport;
	private static Logger logger = Logger.getLogger(TreeTraversal.class.getName());

	private TreeTraversal(Object newToTraverse,
			TreeTraversalSupport newTreeTraversalSupport,
			TreeTraversalCallback newTraversalCallback)
	{
		toTraverse = newToTraverse;
		treeTraversalSupport = newTreeTraversalSupport;
		traversalCallback = newTraversalCallback;
		logger.finer("initialize tree traversal for "+toTraverse);
	}

	public static void traverseOnNewThread(Object newToTraverse,
			TreeTraversalSupport newTreeTraversalSupport,
			TreeTraversalCallback newTraversalCallback)
	{
		new Thread(new TreeTraversal(newToTraverse, newTreeTraversalSupport,
				newTraversalCallback),"TreeTraversal["+newTreeTraversalSupport+","+newTraversalCallback+"]").start();
	}

	public static void traverse(Object newToTraverse,
			TreeTraversalSupport newTreeTraversalSupport,
			TreeTraversalCallback newTraversalCallback)
	{
		new TreeTraversal(newToTraverse, newTreeTraversalSupport, newTraversalCallback) {
		}.run();
	}

	public void run()
	{
		Stack<Object> toVisit = new Stack<Object>();
		toVisit.push(toTraverse);
		double pushCount = 1;
		double popCount = 0;
		logger.finer("starting traversal on thread "+Thread.currentThread());
		traversalCallback.traversalStarted();
		while (!toVisit.isEmpty())
		{
			if(traversalCallback.cancel())
			{
				logger.finer("traversal cancelled by callback");
				return;
			}
			Object popped = toVisit.pop();
			traversalCallback.traversedNode(popped);
			int traversalProgress = (int)Math.ceil(100 * popCount / pushCount);
			traversalCallback.traversalProgress(traversalProgress);
			if(traversalProgress > 0) logger.finest("traversal progress: "+traversalProgress+"%");
			popCount++;
			if (treeTraversalSupport.isLeaf(popped))
			{
				traversalCallback.traversedLeaf(popped);
			}
			else
			{
				traversalCallback.traversedGroup(popped);
				Iterable<?> iterable = treeTraversalSupport.getIterable(popped);
				synchronized(iterable)
				{
					for (Object child : iterable)
					{
						toVisit.push(child);
						pushCount++;
					}
				}
			}
			Thread.yield();
		}
		traversalCallback.traversalProgress(100);
		traversalCallback.traversalCompleted();
		logger.finest("traversal progress: 100%");
		logger.finer("traversal complete.");
	}
}
